#include <jni.h>
#include <string>
#include <android/log.h>

#include "Cangjie.h"

#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "JavaCallC", __VA_ARGS__)


void init_and_load() {
    static bool init = false;
    if (init) {
        return;
    }
    struct RuntimeParam param = {};
    if (InitCJRuntime(&param) != E_OK) {
        LOGE("failed to init. \n");
    }
    if (LoadCJLibraryWithInit("libjavacallcj.so") != E_OK) {
        LOGE("failed to load. \n");
    }
    init = true;
}


extern "C" JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *env, void *reserved) {
    init_and_load();
    return JNI_VERSION_1_6;
}


extern "C" JNIEXPORT jstring JNICALL
Java_com_cjmp_hao_testpkgnative_1windows_EntryEntryAbilityActivity_call_1test_1pkgnative_1windows(
        JNIEnv* env,
        jobject /* this */) {
    void * funcInCj = FindCJSymbol("libjavacallcj.so", "call_test_pkgnative_windows");
    if (funcInCj == nullptr) {
        LOGE("failed to find.\n");
    }
    const char *result = ((const char *(*)(void))funcInCj)();
    return env->NewStringUTF(result);
}

